<?php

namespace App\Admin\Controllers;

use App\Models\Administrator;

use App\Models\Machine;
use App\Models\Operator;
use App\Models\Proxy;
use Encore\Admin\Auth\Database\Role;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class OperatorsController extends Controller
{
    use ModelForm;

    protected $header = '运营管理';
    protected $description = '管理范围内所有机器';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description($this->description);

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description($this->description);

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description($this->description);

            $content->body($this->form());
        });
    }

    public function show(Operator $operator)
    {
        return Admin::content(function (Content $content) use ($operator) {
            $content->header('查看详情');
            $content->body(view('admin.operators.show', ['operator' => $operator]));
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Operator::class, function (Grid $grid) {
            $grid->model()->where('type', Administrator::OPERATOR);

            $grid->username(trans('admin.username'));
            $grid->name(trans('admin.name'));
            $grid->tools(function (Grid\Tools $tools) {
                $tools->batch(function (Grid\Tools\BatchActions $actions) {
                    $actions->disableDelete();
                });
            });
            $grid->actions(function ($actions) {
                $actions->prepend('<a href="' . route('admin.operators.show', [$actions->getKey()]) . '"><i class="fa fa-eye"></i></a>');
            });
            $grid->filter(function ($filter) {
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
            });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Operator::class, function (Form $form) {

            $form->text('username', trans('admin.username'))->rules('required');
            $form->hidden('type', '类型')->value(Administrator::OPERATOR);
            $form->multipleSelect('roles', '角色')->options(Role::query()->where('slug', Administrator::OPERATOR)->get()->pluck('name', 'id'))->rules('required');
            $form->text('name', trans('admin.name'))->rules('required');
            $form->image('avatar', trans('admin.avatar'));
            $form->password('password', trans('admin.password'))->rules('required|confirmed');
            $form->password('password_confirmation', trans('admin.password_confirmation'))->rules('required')
                ->default(function ($form) {
                    return $form->model()->password;
                });

            $form->ignore(['password_confirmation']);
            $form->saving(function (Form $form) {
                if ($form->password && $form->model()->password != $form->password) {
                    $form->password = bcrypt($form->password);
                }
            });
        });
    }
}
